#include "oled.h"


#define LOG_TAG     "oled"     // 该模块对应的标签。不定义时，默认：NO_TAG
#define LOG_LVL     LOG_LVL_DBG   // 该模块对应的日志输出级别。不定义时，默认：调试级别
#include <ulog.h>                 // 必须在 LOG_TAG 与 LOG_LVL 下面
#define OLED_I2C_PIN_SCL                    GET_PIN(A, 2)  // PB6
#define OLED_I2C_PIN_SDA                    GET_PIN(A, 3)  // PB7
u8g2_t u8g2;




/*
 * 可以利用添加标志位强制刷新屏幕与有变化再刷新屏幕相结合实现降低刷屏时的负载和主动刷新的自主性，既降低了负载又不会导致灵活性降低（即无数据更新不会刷新屏幕）
 */
static void oled_entry(void *parameter)
{
    rt_sem_t system_start_up_sem=RT_NULL;
    rt_sem_t system_shutdown_sem=RT_NULL;

    system_start_up_sem=(rt_sem_t)rt_object_find(system_start_up_sem_name, RT_Object_Class_Semaphore);
    if (system_start_up_sem==RT_NULL)
        LOG_W("system_start_up_sem find error");
    system_shutdown_sem=(rt_sem_t)rt_object_find(system_shutdown_sem_name, RT_Object_Class_Semaphore);
    if (system_shutdown_sem==RT_NULL)
        LOG_W("system_shutdown_sem find error");

    rt_event_t tof_event=RT_NULL;
    tof_event=(rt_event_t)rt_object_find(tof_event_name, RT_Object_Class_Event);
    if (tof_event==RT_NULL)
        LOG_W("tof_event find error");

    enum page_number_state{
      page_number_null=0,
      page_number_light_control,
      page_number_Pomodoro
    };
    enum page_number_state page_number=page_number_null;
    enum page_number_state last_page_number=page_number_null;
/*    while(1)
    {
        oled_page_goodbye();
       rt_thread_mdelay(10);
    }*/
    oled_page_hello();
    oled_page_model_init();//开机模型初始化界面
    while(rt_event_recv(tof_event, moxing_init_ok_EVENT_FLAG, RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR, 0, RT_NULL)!=RT_EOK)
    {//等待初始化完成

        rt_thread_mdelay(100);
    }
    LOG_I("recv success moxing_init_ok_EVENT_FLAG");
    u8g2_ClearBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();
    while(1)
    {
        page_number=page_number_null;
        if (rt_sem_trytake(system_start_up_sem)==RT_EOK) {
            //切换到开机界面
            LOG_I("recv success system_start_up_sem");
            oled_page_hello();

        } else if(rt_sem_trytake(system_shutdown_sem)==RT_EOK) {
            //切换到关机页面
            LOG_I("recv success system_shutdown_sem");
            Pomodoro_clear();
            oled_page_goodbye();

        }else if (rt_event_recv(tof_event, up_lx_EVENT_FLAG|down_lx_EVENT_FLAG, RT_EVENT_FLAG_OR, 0, RT_NULL)==RT_EOK) {
            //切换到调节亮度页面
            LOG_I("recv success up_lx_EVENT_FLAG | down_lx_EVENT_FLAG");
            oled_page_light_control();
            page_number=page_number_light_control;
        }else if (rt_event_recv(tof_event, start_Pomodoro_EVENT_FLAG|reset_Pomodoro_EVENT_FLAG|stop_Pomodoro_EVENT_FLAG|clear_Pomodoro_EVENT_FLAG, RT_EVENT_FLAG_OR, 0, RT_NULL)==RT_EOK) {

            //切换到番茄钟界面
            page_number=page_number_Pomodoro;

            if (rt_event_recv(tof_event, start_Pomodoro_EVENT_FLAG, RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR, 0, RT_NULL)==RT_EOK)
            {
                //收到开始番茄钟的事件
                LOG_I("recv success start_Pomodoro_EVENT_FLAG");
                Pomodoro_start();
            }else if (rt_event_recv(tof_event, reset_Pomodoro_EVENT_FLAG, RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR, 0, RT_NULL)==RT_EOK) {
                //收到复位番茄钟/不自动开始的事件
                LOG_I("recv success reset_Pomodoro_EVENT_FLAG");
                Pomodoro_create();
            }else if (rt_event_recv(tof_event, stop_Pomodoro_EVENT_FLAG, RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR, 0, RT_NULL)==RT_EOK) {
                //收到暂停番茄钟的事件
                Pomodoro_stop();
                LOG_I("recv success stop_Pomodoro_EVENT_FLAG");
            }else if (rt_event_recv(tof_event, clear_Pomodoro_EVENT_FLAG, RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR, 0, RT_NULL)==RT_EOK) {
                //收到清除（关闭）番茄钟
                Pomodoro_clear();
                LOG_I("recv success stop_Pomodoro_EVENT_FLAG");
            }
        }else if (rt_event_recv(tof_event, reverse_screen_EVENT_FLAG, RT_EVENT_FLAG_AND|RT_EVENT_FLAG_CLEAR, 0, RT_NULL)==RT_EOK) {
/*            if(page_number==page_number_Pomodoro)
            {
                oled_page_light_control();
                rt_thread_mdelay(1000);
            }

            if(page_number==page_number_light_control)
            {
                oled_page_Pomodoro();
                rt_thread_mdelay(1000);
            }*/

        }

        //

        if (page_number==page_number_light_control) {
            oled_page_light_control();
        }else {
            if (Pomodoro_state_get()==RT_EOK) {
                oled_page_Pomodoro();//若番茄钟定时器存在则显示番茄钟界面
            }else {
                if (last_page_number!=page_number_null && page_number==page_number_null) {
                    //上一个界面有画面，此次界面无画面则清屏
                    //以此避免无效刷屏占用资源
                    u8g2_ClearBuffer(&u8g2);
                    oled_part_bh1750_u8g2_SendBuffer();
                }
            }
        }
        last_page_number=page_number;
        rt_thread_mdelay(100);
    }

}
static int u8g2_sample(void)
{

    rt_thread_t distance_thread;
    LOG_I("u8g2_ssd1306_12864_sw_i2c_example find start!\n");
    distance_thread = rt_thread_create("oled",
                                    oled_entry,
                                     RT_NULL,
                                     2048,
                                     10,
                                     20);
    if (distance_thread != RT_NULL)
    {
        rt_thread_startup(distance_thread);
    }
    return RT_EOK;
}
INIT_COMPONENT_EXPORT(u8g2_sample);//组件初始化,初始化顺序 4


static int rt_u8g2_my_port(void)
{

    u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_rtthread);
    u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_CLOCK, OLED_I2C_PIN_SCL);
    u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_I2C_DATA, OLED_I2C_PIN_SDA);

    u8g2_InitDisplay(&u8g2);
    u8g2_SetPowerSave(&u8g2, 0);
    u8g2_ClearBuffer(&u8g2);
    //u8g2_SendBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();
    LOG_I("u8g2 init success");
    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_u8g2_my_port);
